package com.hyt.it.ogt.kq.main.controller.gov;

import com.alibaba.fastjson.JSON;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.admission.IAdmissionTicketTemplateVariableService;
import com.hyt.it.ogt.kq.service.gov.model.param.AddExamAgreementParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.ExamAgreementVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.agreement.AgreementVO;
import com.hyt.it.ogt.kq.service.gov.service.IExamAgreementService;
import com.hyt.loginfo.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * <p>
 * 考试协议 前端控制器
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@RestController
@Api(tags = "102.考试协议接口", value = "考试协议接口")
@ApiSort(value = 102)
@RequestMapping("/kw/gov/examAgreement")
@Slf4j
public class ExamAgreementController extends BaseController {

    @Resource
    private IExamAgreementService iExamAgreementService;
    @Resource
    private IAdmissionTicketTemplateVariableService iAdmissionTicketTemplateVariableService;

    @ApiOperation(
            value = "2.1 新增考试协议",
            notes = "30202:新增考试协议包异常; 30203:考试协议包只支持zip格式包;30208:上考试协议包文件大小超过限制"
    )
    @ApiOperationSort(value = 1)
    @PostMapping(value = "/add")
    public ApiResponse add(@Valid @RequestBody AddExamAgreementParam param) {
        try {

            iExamAgreementService.add(param);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 新增考试协议包异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 新增考试协议包异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_ADD_AGREEMENT.getCode()).build();
        }
    }

    @ApiOperation(
            value = "2.2 查询所有考试协议",
            notes = "30000:查询所有考试协议成功; 30205:查询所有考试协议异常"
    )
    @ApiOperationSort(value = 2)
    @GetMapping(value = "getAll")
    public ApiResponse<List<AgreementVO>> getAll(@RequestParam String taskId) {
        log.info("# 查询所有考试协议参数 ： {}", taskId);
        try {
            List<AgreementVO> examAgreementList = iExamAgreementService.getAgreementList(taskId);
            return ApiResponse.<List<AgreementVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(examAgreementList)
                    .build();
        } catch (Exception e) {
            log.error("# 查询所有考试协议异常:", e);
            return ApiResponse.<List<AgreementVO>>builder().code(ResponseCode.ERROR_GET_AGREEMENT.getCode()).build();
        }
    }

    @ApiOperation(
            value = "2.3 删除考试协议",
            notes = "30000:删除考试协议成功; 30206:删除考试协议异常; 30204:taskId不能为空"
    )
    @ApiOperationSort(value = 3)
    @DeleteMapping(value = "/deleteByTaskId")
    public ApiResponse deleteByTaskId(@RequestParam String taskId) {
        try {
            //参数校验
            if (StringUtils.isEmpty(taskId)) {
                return ApiResponse.builder().code(ResponseCode.AGREEMENT_TASK_ID_EMPTY.getCode()).build();
            }
            iExamAgreementService.deleteByTaskId(taskId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 删除考试协议失败: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 删除考试协议异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_DELETE_AGREEMENT.getCode()).build();
        }
    }

    @ApiOperation(
            value = "2.4 编辑考试协议",
            notes = "30202:新增考试协议包异常; 30203:考试协议包只支持zip格式包;30208:上考试协议包文件大小超过限制"
    )
    @ApiOperationSort(value = 4)
    @PutMapping(value = "/update")
    public ApiResponse update(@Valid @RequestBody AddExamAgreementParam param) {
        try {
            //参数校验
            iExamAgreementService.updateAgreement(param);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 编辑考试协议包异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 编辑考试协议包异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_UPDATE_AGREEMENT.getCode()).build();
        }
    }

    @ApiOperation(
            value = "2.5 上传考试协议包"
    )
    @ApiOperationSort(value = 5)
    @PostMapping(value = "/uploadAgreementFile")
    public ApiResponse uploadAgreementFile(@RequestParam String taskId, @RequestParam MultipartFile file) {
        try {
            //参数校验
            if (StringUtils.isEmpty(taskId)) {
                return ApiResponse.builder().code(ResponseCode.AGREEMENT_TASK_ID_EMPTY.getCode()).build();
            }
            String result = iExamAgreementService.uploadAgreementFile(file, taskId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).data(result).build();
        } catch (KqException i) {
            log.error("# 上传考试协议包异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 上传考试协议包异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_UPLOAD_AGREEMENT_FILE.getCode()).build();
        }
    }

    @ApiOperation(value = "2.6 上传考生须知图片")
    @ApiOperationSort(value = 6)
    @PostMapping(value = "/uploadAgreementPicture")
    public ApiResponse uploadAgreementPicture(@RequestParam String taskId, @RequestParam MultipartFile file) {
        try {
            String result = iExamAgreementService.uploadAgreementPicture(file, taskId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).data(result).build();
        } catch (KqException i) {
            log.error("# 上传考生须知图片失败: {}", i.getErrCode());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 上传考生须知图片异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_UPLOAD_AGREEMENT_PICTURE.getCode()).build();
        }
    }

    @ApiOperation(value = "2.7 保存考生须知和欢迎语")
    @ApiOperationSort(value = 7)
    @PostMapping("/saveExamNoticeAndWelcomeWords")
    public ApiResponse saveExamNoticeAndWelcomeWords(@RequestBody ExamAgreementVO examAgreementVO) {
        try {
            //参数校验
            if (StringUtils.isBlank(examAgreementVO.getTaskId())) {
                return ApiResponse.builder().code(ResponseCode.AGREEMENT_TASK_ID_EMPTY.getCode()).build();
            }
            if (StringUtils.isBlank(examAgreementVO.getContent())) {
                return ApiResponse.builder().code(ResponseCode.AGREEMENT_IMPORT_TEXT_CONTENT_WRONG.getCode()).build();
            }
            log.info("# 保存考生须知和欢迎语 ： {}", JSON.toJSON(examAgreementVO));
            examAgreementVO.setUpdateBy(this.getUserId());
            log.info("# 保存考生须知和欢迎语 ： {}", JSON.toJSON(examAgreementVO));
            iExamAgreementService.saveExamNoticeAndWelcomeWords(examAgreementVO);

            // 更新考生须知
            User user = getUser();
            try {
                iAdmissionTicketTemplateVariableService.syncExamAgreement(examAgreementVO.getTaskId(), user);
            } catch (Exception e) {
                log.error("[考务][保存考生须知和欢迎语][更新准考证信息]更新考试名称和时间异常，taskId：{}",examAgreementVO.getTaskId(), e);
            }
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 保存考生须知和欢迎语异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 保存考生须知和欢迎语异常:", e);
            return ApiResponse.<List<ExamAgreementVO>>builder().code(ResponseCode.ERROR_SAVE_EXAM_NOTICE_HELLO.getCode()).build();
        }
    }

    @ApiOperation(value = "2.8 查询考生须知和欢迎语")
    @ApiOperationSort(value = 8)
    @GetMapping("/getExamNoticeAndWelcomeWordsList")
    public ApiResponse<List<ExamAgreementVO>> getExamNoticeAndWelcomeWordsList(@RequestParam String taskId) {
        try {
            log.info("# 获取考生须知和欢迎语列表 ： {}" + taskId);
            List<ExamAgreementVO> examAgreementList = iExamAgreementService.getExamNoticeAndWelcomeWordsList(taskId);
            return ApiResponse.<List<ExamAgreementVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(examAgreementList)
                    .build();
        } catch (Exception e) {
            log.error("# 查询考生须知和欢迎语列表异常:", e);
            return ApiResponse.<List<ExamAgreementVO>>builder().code(ResponseCode.ERROR_GET_NOTICE_HELLO.getCode()).build();
        }
    }

}
